<?php

namespace app\common\controller;

use think\facade\Db;
/**
 *  水井电闸API
 */
class WaterPump
{
    public $appKey = "18600636500";
    public $appSecret = "123456";

//    public function __construct($app_key='', $app_secret='')
//    {
//        if($app_key!='') $this->app_key = $app_key;
//        if($app_secret!='') $this->app_secret = $app_secret;
//        $this->charset = 'utf-8';
//        list($msec, $sec) = explode(' ', microtime());
//        $this->time = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
//    }

    //指针喷灌登录
    public function getToken()
    {
        $loginInfo = Db::name('config')->where('name','pen_guan_login_token')->find();
        if (isset($loginInfo['value']) && !empty($loginInfo['value'])) {
            return $loginInfo['value'];
        }
        $url = "https://api.zesi.com .cn/api/auth/login";
        $postData = [
            'phone'=>$this->userName,
            'password'=>$this->password,
        ];
        $result = $this->curlPost($url,$postData);
        $loginArr = json_decode($result,true);
        $token = '';
        if (isset($loginArr['data']['token']) && !empty($loginArr['data']['token'])) {
            Db::name('config')->insert(['name'=>'pen_guan_login_token','value'=>$loginArr['data']['token']]);
            $token = $loginArr['data']['token'];
        }
        return $token;
    }

    //获取设备列表
    public function getDeviceList()
    {
        $token = $this->getToken();
        $url = "https://api.zesi.com.cn/api/devices";
        $options = array(
            CURLOPT_HTTPHEADER => array(
                "token:".$token
            )
        );
        $result = $this->curlGet($url,$options);
        return json_decode($result,true);
    }

    //获取设备详情
    public function getDeviceDetail()
    {
        $token = $this->getToken();
        $url = "https://api.zesi.com.cn/api/devices/0112.0007.2333004348";
        $options = array(
            CURLOPT_HTTPHEADER => array(
                "token:".$token
            )
        );
        $result = $this->curlGet($url,$options);
        return json_decode($result,true);
    }

    //获取设备实时状态
    public function getDeviceStatus($serialno)
    {
        $token = $this->getToken();
        $url = "https://api.zesi.com.cn/api/devices/states/".$serialno;
        $options = array(
            CURLOPT_HTTPHEADER => array(
                "token:".$token
            )
        );
        $result = $this->curlGet($url,$options);
        return json_decode($result,true);
    }


    /**
     * 设备操作
     *
     */
    public function operateDevice($serialno)
    {
        $token = $this->getToken();
        $url = "https://api.zesi.com.cn/api/device/".$serialno."/sendCommand";
        $postData = [];
        $options = array(
            CURLOPT_HTTPHEADER => array(
                "token:".$token
            )
        );
        $result = $this->curlPost($url,$postData,$options);
        return json_decode($result,true);
    }


    public function curlPost($url = '', $postData = '', $options = array())
    {
        if (is_array($postData)) {
            $postData = http_build_query($postData);
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
        if (!empty($options)) {
            curl_setopt_array($ch, $options);
        }
        //https请求 不验证证书和host
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

    public function curlGet($url, $options=[])
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Mobile Safari/537.36');
        if (!empty($options)) {
            curl_setopt_array($ch, $options);
        }
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
}

